package com.karashi.twitter;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.karashi.R;
import com.karashi.twitter.task.DownloadTaskForImage;

public class TwitterArrayAdapter extends ArrayAdapter<TwitterStatus> {

	private ArrayList<TwitterStatus> mItems;
	private LayoutInflater mInflater;

	public TwitterArrayAdapter(Context context, int resource, ArrayList<TwitterStatus> items) {
		super(context, resource, items);

		mItems = items;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = convertView;
		if (view == null) {
			// 新たにviewを作成
			view = mInflater.inflate(R.layout.twitter_row, null);
		}

		TwitterStatus item = (TwitterStatus) mItems.get(position);
		if (item != null) {
			TextView screenName = (TextView) view.findViewById(R.id.toptext);
			screenName.setTypeface(Typeface.DEFAULT_BOLD);

			TextView text = (TextView) view.findViewById(R.id.bottomtext);

			TextView createAt = (TextView) view.findViewById(R.id.create_at);

			if (screenName != null) {
				screenName.setText(item.getScreenName());
			}

			if (text != null) {
				text.setText(item.getText());
			}

			if(createAt != null){
				createAt.setText(twitterHumanFriendlyDate(item.getCreateAt()));
			}

			// バックグラウンドで画像取得
			ImageView imageView = (ImageView) view.findViewById(R.id.icon);
			DownloadTaskForImage task = new DownloadTaskForImage(imageView);
			task.execute(item.getProfileImageUrl());

		}

		// 背景画像をセット
		view.setBackgroundResource(R.drawable.back);

		return view;
	}

	private String twitterHumanFriendlyDate(Date date) {
		// today
		Date today = new Date();

		// how much time since (ms)
		Long duration = today.getTime() - date.getTime();

		int second = 1000;
		int minute = second * 60;
		int hour = minute * 60;
		int day = hour * 24;

		if (duration < second * 7) {
			return "right now";
		}

		if (duration < minute) {
			int n = (int) Math.floor(duration / second);
			return n + " seconds ago";
		}

		if (duration < minute * 2) {
			return "about 1 minute ago";
		}

		if (duration < hour) {
			int n = (int) Math.floor(duration / minute);
			return n + " minutes ago";
		}

		if (duration < hour * 2) {
			return "about 1 hour ago";
		}

		if (duration < day) {
			int n = (int) Math.floor(duration / hour);
			return n + " hours ago";
		}
		if (duration > day && duration < day * 2) {
			return "yesterday";
		}

		if (duration < day * 365) {
			int n = (int) Math.floor(duration / day);
			return n + " days ago";
		} else {
			return "over a year ago";
		}
	}

}
